package client.main.choose;

import client.advantagesuggestion.FeedbackForm;
import client.bugreport.BugReporting;
import client.main.login.Login;
import client.material.MaterialUserArchiveQuery;
import client.order.SubmitOrderForm;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class UserChoose extends JFrame {
    public UserChoose() {
        setTitle("用户功能选择");
        setSize(800, 600); // 设置窗口大小为800x600
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        
        setLocationRelativeTo(null);
        setResizable(false);
        
        String path = System.getProperty("user.dir");
        String imageAbsolutePath = path + "\\background\\image1.jpg";
        System.out.println("imageAbsolutePath: " + imageAbsolutePath);
        
        // 设置背景面板
        BackgroundPanel backgroundPanel = new BackgroundPanel(imageAbsolutePath); // 设置背景图片路径
        backgroundPanel.setLayout(new BoxLayout(backgroundPanel, BoxLayout.Y_AXIS)); // 垂直布局
        
        // 创建标题标签
        JLabel titleLabel = new JLabel("请选择功能", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 30)); // 设置标题字体和大小
        titleLabel.setForeground(Color.decode("#424242")); // 设置标题颜色为深灰色
        
        // 使用一个容器将标题标签包裹起来，以便能够在水平方向上居中
        JPanel titlePanel = new JPanel();
        titlePanel.setOpaque(false); // 设置为透明
        titlePanel.setLayout(new BoxLayout(titlePanel, BoxLayout.X_AXIS)); // 横向布局
        titlePanel.add(Box.createHorizontalGlue()); // 添加左侧弹性空间
        titlePanel.add(titleLabel); // 添加标题标签
        titlePanel.add(Box.createHorizontalGlue()); // 添加右侧弹性空间
        
        backgroundPanel.add(titlePanel); // 将标题面板添加到背景面板
        backgroundPanel.add(Box.createRigidArea(new Dimension(0, 40))); // 标题与按钮之间的间距
        
        
        // 创建按钮面板，并设置为 GridBagLayout 布局
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        buttonPanel.setOpaque(false); // 设置面板背景透明
        
        // 定义 GridBagConstraints 对象用于布局控制
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL; // 水平方向填充
        gbc.insets = new Insets(15, 20, 15, 20); // 上、左、下、右的外边距
        
        // 创建物料档案管理按钮
        gbc.gridx = 0;
        gbc.gridy = 0;
        JButton materialUserArchiveQueryButton = createCustomButton("物料档案管理");
        materialUserArchiveQueryButton.addActionListener(e -> openMaterialUserArchiveQuery());
        buttonPanel.add(materialUserArchiveQueryButton, gbc);
        
        // 创建提交订单按钮
        gbc.gridx = 1;
        gbc.gridy = 0;
        JButton submitOrderFormButton = createCustomButton("提交订单管理");
        submitOrderFormButton.addActionListener(e -> openSubmitOrderForm());
        buttonPanel.add(submitOrderFormButton, gbc);
        
        gbc.gridx = 0;
        gbc.gridy = 1;
        JButton BugReportDialog = createCustomButton("bug反馈");
        BugReportDialog.addActionListener(e -> openBugReportDialog());
        buttonPanel.add(BugReportDialog, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        JButton Advantagesuggesting = createCustomButton("优化建议");
        Advantagesuggesting.addActionListener(e -> openAdvantagesuggesting());
        buttonPanel.add(Advantagesuggesting, gbc);
        
        // 创建返回登录按钮并添加到底部
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 1; // 使返回登录按钮占用两列
        JButton loginButton = createCustomButton("返回登录");
        loginButton.addActionListener(e -> openLogin());
        buttonPanel.add(loginButton, gbc);
        
        
        // 将按钮面板添加到背景面板
        backgroundPanel.add(buttonPanel);
        
        // 设置窗口可见
        setContentPane(backgroundPanel); // 设置背景面板为主内容面板
        setVisible(true);
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(UserChoose::new); // 使用 SwingUtilities 确保线程安全
    }
    
    // 创建自定义按钮的辅助方法（带圆角、渐变背景和边框效果）
    private JButton createCustomButton(String text) {
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                if (g instanceof Graphics2D) {
                    Graphics2D g2d = (Graphics2D) g;
                    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    
                    // 设置按钮的渐变填充颜色，从淡绿色到深绿色
                    GradientPaint gradientPaint = new GradientPaint(0, 0, new Color(144, 238, 144), 0, getHeight(), new Color(34, 139, 34));
                    g2d.setPaint(gradientPaint);
                    g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 30, 30); // 设置圆角半径为30
                }
                super.paintComponent(g);
            }
            
            @Override
            protected void paintBorder(Graphics g) {
                g.setColor(new Color(21, 101, 192)); // 设置边框颜色
                ((Graphics2D) g).setStroke(new BasicStroke(2)); // 设置边框宽度
                g.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 30, 30); // 圆角边框
            }
        };
        
        // 按钮的其他样式设置
        button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        button.setForeground(Color.WHITE); // 按钮文本颜色
        button.setFocusPainted(false); // 去掉按钮焦点边框
        button.setContentAreaFilled(false); // 使按钮透明
        button.setPreferredSize(new Dimension(200, 50)); // 按钮尺寸
        button.setOpaque(false); // 按钮透明
        button.setBorderPainted(false); // 去除边框
        button.setCursor(new Cursor(Cursor.HAND_CURSOR)); // 鼠标悬停时显示手型
        
        // 鼠标悬停事件，改变按钮颜色
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent e) {
                button.setBackground(new Color(30, 136, 229)); // 鼠标悬停时背景色
                button.setForeground(new Color(255, 255, 255)); // 鼠标悬停时文本颜色
                button.setOpaque(true); // 透明度设为不透明
            }
            
            @Override
            public void mouseExited(java.awt.event.MouseEvent e) {
                button.setOpaque(false); // 鼠标离开时恢复透明度
            }
        });
        
        return button;
    }
    
    // 打开提交订单窗口
    private void openSubmitOrderForm() {
        new SubmitOrderForm();
    }
    
    // 打开物料档案管理窗口
    private void openMaterialUserArchiveQuery() {
        new MaterialUserArchiveQuery().setVisible(true);
    }
    
    // 打开bug反馈窗口
    private void openBugReportDialog() {
        new BugReporting().setVisible(true);
    }
    
    // 打开优化建议窗口
    private void openAdvantagesuggesting() {
        new FeedbackForm();
    }
    
    // 打开登录界面的窗口
    private void openLogin() {
        new Login(); // 打开 Login 窗口
        dispose(); // 关闭当前窗口
    }
    
    // 自定义背景面板类
    private static class BackgroundPanel extends JPanel {
        private Image backgroundImage;
        
        public BackgroundPanel(String imagePath) {
            try {
                backgroundImage = ImageIO.read(new File(imagePath));
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("背景图片加载失败！");
            }
        }
        
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (backgroundImage != null) {
                g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
            }
        }
    }
}
